package com.siwei.infoextract.wrapper.attributeextract;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.siwei.infoextract.rule.RuleEngine;
import com.siwei.infoextract.wrapper.parser.DomNode;
import com.siwei.infoextract.wrapper.parser.DomNodeIteratorCallback;
import com.siwei.infoextract.wrapper.parser.DomTreeTraversal;


public abstract class AttributeExtractor {
	RuleEngine ruleEngine;

	public List<DomNode> extract(DomNode domNode) throws IOException {
		ruleEngine.apply(domNode);

		final List<DomNode> results = new ArrayList<DomNode>();

		DomTreeTraversal traversal = new DomTreeTraversal(domNode); 
		traversal.addCallback(new DomNodeIteratorCallback(){
			public void call(DomNode domNode) {
				Map<String, String> map = (Map<String, String>)domNode.getFlag();
				String predict = map.get(ruleEngine.getName());

				if (predict != null) {
					if (Boolean.parseBoolean(predict) == true) {
						results.add(domNode);
					}
				}
			}
		});
		traversal.traverse();		

		return results;
	}
}
